﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using test; 
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using System.Reflection;


namespace SystemContainer.Models
{
    public class MatlabWrapper
    {

        public static string[] getNearByPhotos(Location location, String algorithmName, String datasetDir) 
        {
            string[] photos = null;
            object[] parameters = new object[3]{location.Latitude, location.Longtitude, datasetDir};

            ///MWNumericArray loc = new double[2] {location.Latitude, location.Longtitude};

            ///MWNumericArray output = null;
            //TODO: dynamic compiling &  algorthm implementation
            //the class provided by matlab should accept 2 parameter:
            //1. loc which contains lat and lon
            //2. directory of feature file ,which would be a list of feature vector + lat & lon + url
            //result returned should be a list of photo urls
            
            ///mksqr sqr = new mksqr();

            ///MWArray[] result = sqr.makesquare(1,loc);
            ///output = (MWNumericArray)result[0];

            Object obj = getAlgorithmObject(algorithmName);
           Type t = obj.GetType();

            //TODO matlab实现的方法必须名为getNearByPhotos
            try
            {
                photos = (string[])t.InvokeMember("getNearByPhotos", BindingFlags.InvokeMethod, null, obj, parameters);
            }
            catch (Exception)
            {
                Console.Write("can't convert to string[]");
            }
            return photos;
            
        }

        private static Object getAlgorithmObject(String classname)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            Object obj = asm.CreateInstance(classname, true);
            return obj;
        }


        //getAOI
        public static Place[] getHotPlaces(double minLat, double MaxLat, double MinLon, double MaxLon)
        {
            Place[] hotPlaces = null;

            //TODO: algorthm implementation

            return hotPlaces;
        
        }
    }
}